package com.ifast.hnzk.controller;


import java.util.Arrays;
import java.util.Date;
import java.util.List;

import com.ifast.common.domain.DictDO;
import com.ifast.common.service.DictService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.ifast.common.annotation.Log;
import com.ifast.common.base.BaseController;
import com.ifast.hnzk.domain.CustomerDO;
import com.ifast.hnzk.service.CustomerService;
import com.ifast.common.utils.Result;

/**
 * 
 * <pre>
 * 
 * </pre>
 * <small> 2019-07-03 11:20:11 | wch</small>
 */
@Controller
@RequestMapping("/hnzk/customer")
public class CustomerController extends BaseController {
	@Autowired
	private CustomerService customerService;
	@Autowired
	private DictService dictService;

	@GetMapping()
	@RequiresPermissions("hnzk:customer:customer")
	String Customer(){
	    return "hnzk/customer/customer";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("hnzk:customer:customer")
	public Result<Page<CustomerDO>> list(CustomerDO customerDTO){
        Wrapper<CustomerDO> wrapper = new EntityWrapper<CustomerDO>();
        wrapper.like("name",customerDTO.getName());
		wrapper.eq(customerDTO.getStatus()!=null,"status",customerDTO.getStatus());
		wrapper.eq(customerDTO.getSignStatus()!=null,"signStatus",customerDTO.getSignStatus());
		wrapper.orderBy("id", false);
        Page<CustomerDO> page = customerService.selectPage(getPage(CustomerDO.class), wrapper);
        return Result.ok(page);
	}
	
	@GetMapping("/add")
	@RequiresPermissions("hnzk:customer:add")
	String add(Model model){
		/**
		 * 查询性别列表
		 */
		Wrapper<DictDO> wrapper = new EntityWrapper<DictDO>();
		wrapper.eq("type","sex");
		List<DictDO> dictList = dictService.selectList(wrapper);
		model.addAttribute("dictList",dictList);
	    return "hnzk/customer/add";
	}

	@GetMapping("/edit/{id}")
	@RequiresPermissions("hnzk:customer:edit")
	String edit(@PathVariable("id") Long id,Model model){
		CustomerDO customer = customerService.selectById(id);
		model.addAttribute("customer", customer);
	    return "hnzk/customer/edit";
	}
	
	@Log("添加")
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("hnzk:customer:add")
	public Result<String> save( CustomerDO customer){
		customer.setCreatDate(new Date());
		customer.setEditDate(new Date());
		customer.setRevisor(getUserId().toString());
		customer.setCreator(getUserId().toString());
		customer.setTrackingNumber(1);
		customer.setIntegral(0L);
		customerService.insert(customer);
        return Result.ok();
	}
	
	@Log("修改")
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("hnzk:customer:edit")
	public Result<String>  update( CustomerDO customer){
		customer.setEditDate(new Date());
		customer.setRevisor(getUserId().toString());
		boolean update = customerService.updateById(customer);
		return update ? Result.ok() : Result.fail();
	}
	
	@Log("删除")
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("hnzk:customer:remove")
	public Result<String>  remove( Long id){
		customerService.deleteById(id);
        return Result.ok();
	}
	
	@Log("批量删除")
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("hnzk:customer:batchRemove")
	public Result<String>  remove(@RequestParam("ids[]") Long[] ids){
		customerService.deleteBatchIds(Arrays.asList(ids));
		return Result.ok();
	}
	
}
